(function(jQuery) {
	var complex_search_pageId;
	var complex_search_gridId;
	var complex_search_url;
	/*
	 * 高级查询JS插件
	 */
	
	$.ComplexSearchUtils = {

			/**
			 * 根据查询条件刷新DataGrid
			 */
			refreshDataGrid : function() {
				// WHERE 条件
				var whereStr = "";
				var dataDicValue;
				
				// 获取查询条件
				var conditionRows = $('#condition_dg_complex').datagrid('getRows');
				for (var i = 0 ; i < conditionRows.length; i++) {
					var conditionRow = conditionRows[i];
					
					var columnName = conditionRow.columnName;
					var columnType = conditionRow.columnType;
					var condition = conditionRow.condition;
					var value1 = conditionRow.value1;
					var value2 = conditionRow.value2;
					var andor = conditionRow.andor;
					
					switch (andor) {
					case "并且" :
						whereStr = whereStr + " AND " + $.ComplexSearchUtils.cerateSQLWhere(columnName, columnType, condition, value1, value2);						
						break;
					case "或者" :
						whereStr = whereStr + " OR " + $.ComplexSearchUtils.cerateSQLWhere(columnName, columnType, condition, value1, value2);
						break;
					default:
						whereStr = whereStr + " " + $.ComplexSearchUtils.cerateSQLWhere(columnName, columnType, condition, value1, value2);
						break;
					}
				}
		
				// 设置URL
				$(complex_search_gridId).datagrid('options').url = complex_search_url;
				
				// 设置参数
				var param = {};
				whereStr = whereStr.replace(/(^ AND|^ OR)/g, "");
				param.conditions = encodeURI(whereStr);
				param.Login_SessionId = sessionId;
				param.readOnly = 'YES';
				
				// 请求数据
				$(complex_search_gridId).datagrid("load", param);
				$(complex_search_gridId).datagrid("clearSelections");
				
				$("#complex_search_window").window('close');
				
			},
			
			/**
			 * 生成一行的WHERE 条件
			 */
			cerateSQLWhere : function(colName, columnType, condition, value1, value2) {
				var whereStr = null;
				
				var columnName = "{" + colName + "}"
				
				switch (condition) {
				case "等于" :
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') = '" + value1 + "'";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') = '" + value1 + "'";
						break;
					case "CHAR" :
							whereStr = columnName + " = '" + value1 + "' ";
						break;
					case "NUMBER" :
						whereStr = columnName + " = " + value1;
						break;
					}
					
					break;
				case "不等于" :
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') <> '" + value1 + "'";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') <> '" + value1 + "'";
						break;
					case "CHAR" :
							whereStr = columnName + " <> '" + value1 + "' ";
						break;
					case "NUMBER" :
						whereStr = columnName + " <> " + value1;
						break;
					}
					break;
					
				case "大于" :
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') > '" + value1 + "'";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') > '" + value1 + "'";
						break;
					case "CHAR" :
						whereStr = columnName + " > '" + value1 + "' ";
						break;
					case "NUMBER" :
						whereStr = columnName + " > " + value1;
						break;
					}
					break;
					
				case "大于等于" :
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') >= '" + value1 + "'";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') >= '" + value1 + "'";
						break;
					case "CHAR" :
						whereStr = columnName + " >= '" + value1 + "' ";
						break;
					case "NUMBER" :
						whereStr = columnName + " >= " + value1;
						break;
					}
					break;
					
				case "小于" :
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') < '" + value1 + "'";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') < '" + value1 + "'";
						break;
					case "CHAR" :
						whereStr = columnName + " < '" + value1 + "' ";
						break;
					case "NUMBER" :
						whereStr = columnName + " < " + value1;
						break;
					}
					break;

				case "小于等于" :
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') <= '" + value1 + "'";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') <= '" + value1 + "'";
						break;
					case "CHAR" :
						whereStr = columnName + " <= '" + value1 + "' ";
						break;
					case "NUMBER" :
						whereStr = columnName + " <= " + value1;
						break;
					}
					break;

				case "为空" :
					whereStr = columnName + " IS NULL ";
					break;

				case "不为空" :
					whereStr = columnName + " IS NOT NULL ";
					break;
					
				case "包含" :
					var inStr = value1.replace(new RegExp(",","gm"), "','");
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') IN ('" + inStr + "')";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') IN ('" + inStr + "')";
						break;
					case "CHAR" :
						whereStr = columnName + " IN ('" + inStr + "') ";
						break;
					case "NUMBER" :
						whereStr = columnName + " IN (" + inStr + ")";
						break;
					}
					break;

				case "不包含" :
					var inStr = value1.replace(new RegExp(",","gm"), "','");
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') NOT IN ('" + inStr + "')";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') NOT IN ('" + inStr + "')";
						break;
					case "CHAR" :
						whereStr = columnName + " NOT IN ('" + inStr + "') ";
						break;
					case "NUMBER" :
						whereStr = columnName + " NOT IN (" + inStr + ")";
						break;
					}
					break;

				case "全模糊" :
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') LIKE '%" + value1 + "%'";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') LIKE '%" + value1 + "%'";
						break;
					case "CHAR" :
						whereStr = columnName + " LIKE '%" + value1 + "%'";
						break;
					case "NUMBER" :
						whereStr = columnName + " LIKE '%" + value1 + "%'";
						break;
					}
					break;

				case "左模糊" :
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') LIKE '%" + value1 + "'";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') LIKE '%" + value1 + "'";
						break;
					case "CHAR" :
						whereStr = columnName + " LIKE '%" + value1 + "'";
						break;
					case "NUMBER" :
						whereStr = columnName + " LIKE '%" + value1 + "'";
						break;
					}
					break;
					
				case "右模糊" :
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') LIKE '" + value1 + "%'";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') LIKE '" + value1 + "%'";
						break;
					case "CHAR" :
						whereStr = columnName + " LIKE '" + value1 + "%'";
						break;
					case "NUMBER" :
						whereStr = columnName + " LIKE '" + value1 + "%'";
						break;
					}
					break;

				case "介于...之间" :
					switch (columnType) {
					case "DATE" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d') BETWEEN '" + value1 + "' AND '" + value2 + "' ";
						break;
					case "DATETIME" :
						whereStr = "DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') BETWEEN '" + value1 + "' AND '" + value2 + "' ";
						break;
					case "CHAR" :
						whereStr = columnName + " BETWEEN '" + value1 + "' AND '" + value2 + "' ";
						break;
					case "NUMBER" :
						whereStr = columnName + " BETWEEN " + value1 + " AND " + value2 + " ";
						break;
					}
					break;
				}
				
				return whereStr;
				
			},
			
			/**
			 * 保存既有预定义查询
			 */
			savePreDefineSrch : function() {
				var selectedRow = $('#predefine_dg_complex').datagrid('getSelected');
				if (selectedRow) {
					var srchName = selectedRow.queryName;
					var userCode = selectedRow.userCode;
					var pageId = selectedRow.pageId;
					var conditionRows = $('#condition_dg_complex').datagrid('getRows');
					
					var effectRow = new Object();
					effectRow["conditionRows"] = JSON.stringify(conditionRows);
					effectRow["pageId"] = pageId;
					effectRow["userCode"] = userCode;
					effectRow["queryName"] = srchName;
					effectRow["Login_SessionId"] = sessionId;
					
					// 调用服务端保存
					$.post("seniorQuery/savePreDefineSrch.json", effectRow, function(data) {
				    	$.LmisUtils.handleActionResult(data);
						// 刷新数据
						var param = {
								userCode : userCode,
								pageId : pageId,
								Login_SessionId: sessionId
							};
						
						$('#predefine_dg_complex').datagrid("load", param);
						$.ComplexSearchUtils.resetPreDefineCondition();
						
					}, "JSON");

				} else {
					// 新增的预定义查询，弹出“输入查询名对话框，输入预定义查询名”
					$('#input_searchName_dlg').dialog('open');
				}
			},
			
			/**
			 * 另存既有预定义查询
			 */
			savePreDefineSrchAs : function() {
				// 另存的预定义查询，弹出“输入查询名对话框，输入预定义查询名”
				$('#input_searchName_dlg').dialog('open');
			},

			/**
			 * 保存新建预定义查询(点击预定义查询名字对话框的“确定”按钮时调用)
			 */
			saveNewDefineSrch : function() {
				var userCode = null;
				// 取得查询名
				var srchName = $('#complex_search_name').val();
				// 取得所有查询条件
				var conditionRows = $('#condition_dg_complex').datagrid('getRows');
				var effectRow = new Object();
				effectRow["conditionRows"] = JSON.stringify(conditionRows);
				effectRow["pageId"] = complex_search_pageId;
				effectRow["userCode"] = userCode;
				effectRow["queryName"] = srchName;
				effectRow["Login_SessionId"] = sessionId;
	
				// 调用服务端保存
				$.post("seniorQuery/savePreDefineSrch.json", effectRow, function(data) {
			    	$.LmisUtils.handleActionResult(data);
			    	if (data.retcode == 0) {
						// 刷新数据
						var param = {
								userCode : userCode,
								pageId : complex_search_pageId,
								Login_SessionId: sessionId
							};
						
						$('#predefine_dg_complex').datagrid("load", param);
						$.ComplexSearchUtils.resetPreDefineCondition();
			    	}
				}, "JSON");
	
				// 关闭对话框
				$('#input_searchName_dlg').dialog('close');
				
		    },
			
			/**
			 * 删除预定义查询
			 */
			deletePreDefineSrch : function() {
				var selectedRow = $('#predefine_dg_complex').datagrid('getSelected');
				// 没选择数据时，显示告警，返回
				if (!selectedRow) {
					$.messager.show({
						title: $.i18n.prop("title"),
						msg: '请选择预定义查询',
						showType: 'slide',
						timeout: 2000,
						style:{
							right:'',
							top:document.body.scrollTop+document.documentElement.scrollTop,
							bottom:''
						}
					});		
					
					return false;
				}
				
				
				$.messager.confirm("操作提示", "确实要执行删除功能吗？", function (ok) { 
		            if (ok) { 
		        		// 设置要删除数据
		            	var id = selectedRow.id;
		            	var param = {
		            			searchId : id,
		            			Login_SessionId : sessionId
							};
		        		
		        		// 发送请求
		        		$.post("seniorQuery/deletePreDefineSrch.json", param, function(data) {
		        	    	$.LmisUtils.handleActionResult(data);
					    	if (data.retcode == 0) {
								// 刷新数据
								var param = {
										userCode : null,
										pageId : complex_search_pageId,
										Login_SessionId : sessionId
									};
								
								$('#predefine_dg_complex').datagrid("load", param);
								$.ComplexSearchUtils.resetPreDefineCondition();
					    	}
		        		}, "JSON");
		        		
		            } 
		        }); 
				
			},
			
			/**
			 * 清空预定义查询条件
			 */
			resetPreDefineCondition : function() {
				// Clear Condition DataGrid
				var rowCount = $('#condition_dg_complex').datagrid('getRows').length;
				for (var i = rowCount - 1; i >= 0; i--) {
					$('#condition_dg_complex').datagrid('deleteRow', i);
				}
				
				// Clear Condition edit area
				document.getElementById("sel_colName").innerHTML="";
				document.getElementById("sel_compareName").innerHTML="";
				$("#sel_colValue1").val("");
				$("#sel_colValue2").val("");
				$("#div_sel_colValue1").css("display","block");
				$("#div_sel_colValue2").css("display","block");
				$("#div_sel_dateFrom").css("display","none");
				$("#div_sel_dateTo").css("display","none");
				$("#div_sel_dateTimeFrom").css("display","none");
				$("#div_sel_dateTimeTo").css("display","none");
				
				
			},			
			
			/**
			 * 删除预定义查询条件
			 */
			deletePreDefineCondition : function() {
				var selectedRow = $('#condition_dg_complex').datagrid('getSelected');
				// 没选择数据时，显示告警，返回
				if (!selectedRow) {
					$.messager.show({
						title: $.i18n.prop("title"),
						msg: '请选择查询条件',
						showType: 'slide',
						timeout: 2000,
						style:{
							right:'',
							top:document.body.scrollTop+document.documentElement.scrollTop,
							bottom:''
						}
					});		
					
					return false;
				}
				
				
				$.messager.confirm("操作提示", "确实要执行删除功能吗？", function (ok) { 
		            if (ok) { 
						// 取得选择行
						var selectedRow = $('#condition_dg_complex').datagrid('getSelected');
						var index = $('#condition_dg_complex').datagrid('getRowIndex', selectedRow);

						// 删除选择行
						$('#condition_dg_complex').datagrid('deleteRow', index);
		            } 
		        }); 
				
			},
			
			/**
			 * 创建比较符数据
			 */
			getColType : function() {
				var columnName = $("#complex_search_colName").combobox('getValue');
				var colDatas = $("#complex_search_colName").combobox('getData');
				var columnType;
				for (var i = 0; i < colDatas.length; i++) {
					if (columnName == colDatas[i].columnName) {
						columnType = colDatas[i].type;
						break;
					}
				}
				
				// 返回
				return columnType;
			},
			
			
			/**
			 * 创建比较符数据
			 */
			createCompareSigns : function() {
				var signLst = [{
								name: '等于',
								value:  '等于'
							  },{
									name: '不等于',
									value:  '不等于'
							  },{
									name: '大于',
									value:  '大于'
							  },{
									name: '大于等于',
									value:  '大于等于'
							  },{
									name: '小于',
									value:  '小于'
							  },{
									name: '小于等于',
									value:  '小于等于'
							  },{
									name: '为空',
									value:  '为空'
							  },{
									name: '不为空',
									value:  '不为空'
							  },{
									name: '包含',
									value:  '包含'
							  },{
									name: '不包含',
									value:  '不包含'
							  },{
									name: '全模糊',
									value:  '全模糊'
							  },{
									name: '左模糊',
									value:  '左模糊'
							  },{
									name: '右模糊',
									value:  '右模糊'
							  },{
									name: '介于...之间',
									value:  '介于...之间'
							  }];
				
				// 返回
				return signLst;
			},
			

			/**
			 * 增加查询公式
			 */
			addCondition : function(andor){
				var value1,value2;
				
				if ($('#div_complex_search_colValue1').css("display")!="none") {
					var validate = $('#complex_search_colValue1').validatebox("isValid");
					if (!validate) {
						return false;
					} else {
						value1 = $('#complex_search_colValue1').val();
					}
				}
				
				if ($('#div_complex_search_colValue2').css("display")!="none") {
					var validate = $('#complex_search_colValue2').validatebox("isValid");
					if (!validate) {
						return false;
					} else {
						value2 = $('#complex_search_colValue2').val();
					}
				}
				
				if ($('#div_complex_search_dateFrom').css("display")!="none") {
					var validate = $('#complex_search_dateFrom').datebox("isValid");
					if (!validate) {
						return false;
					} else {
						value1 = $('#complex_search_dateFrom').datebox('getValue');
					}
				}
				
				if ($('#div_complex_search_dateTo').css("display")!="none") {
					var validate = $('#complex_search_dateTo').datebox("isValid");
					if (!validate) {
						return false;
					} else {
						value2 = $('#complex_search_dateTo').datebox('getValue');
					}
				}
				
				if ($('#div_complex_search_dateTimeFrom').css("display")!="none") {
					var validate = $('#complex_search_dateTimeFrom').datetimebox("isValid");
					if (!validate) {
						return false;
					} else {
						value1 = $('#complex_search_dateTimeFrom').datetimebox('getValue');
					}
				}
				
				if ($('#div_complex_search_dateTimeTo').css("display")!="none") {
					var validate = $('#complex_search_dateTimeTo').datetimebox("isValid");
					if (!validate) {
						return false;
					} else {
						value2 = $('#complex_search_dateTimeTo').datetimebox('getValue');
					}
				}

				if ($('#div_complex_search_dataDic').css("display")!="none") {
					var validate = $('#complex_search_dataDic').combobox("isValid");
					if (!validate) {
						return false;
					} else {
						value1 = $('#complex_search_dataDic').combobox('getValue');
					}
				}
				// 查询Id
				var id;

				// 取得选择行
				var selectedRow = $('#predefine_dg_complex').datagrid('getSelected');

				// 没选择预定义查询时，查询Id为空
				if (!selectedRow) {
					// 取得查询Id
					id = null;
				} else {
					// 取得查询Id
					id = selectedRow.id;
				}

				// 增加一行查询条件
				var columnName = $("#complex_search_colName").combobox('getValue');
				var condition = $("#complex_search_compare").combobox('getValue');
				var columnType = $.ComplexSearchUtils.getColType();
				var rowNum = $('#condition_dg_complex').datagrid('getRows').length;
				if (rowNum <= 0) {
					andor = '';
				}
				
				$('#condition_dg_complex').datagrid('appendRow', {"id":id,"columnName":columnName,"columnType":columnType,"condition":condition,"value1":value1,"value2":value2,"andor":andor});
			},

			/**
			 * 更新查询公式
			 */
			updateCondition : function(){
				var row = $('#condition_dg_complex').datagrid('getSelected');
				if (row) {
					if ($('#div_sel_colValue1').css("display")!="none") {
						var validate = $('#sel_colValue1').validatebox("isValid");
						if (!validate) {
							return false;
						} else {
							row.value1 = $("#sel_colValue1").val();
						}
					}

					if ($('#div_sel_colValue2').css("display")!="none") {
						var validate = $('#sel_colValue2').validatebox("isValid");
						if (!validate) {
							return false;
						} else {
							row.value2 = $("#sel_colValue2").val();
						}
					}
					
					if ($('#div_sel_dateFrom').css("display")!="none") {
						var validate = $('#sel_dateFrom').datebox("isValid");
						if (!validate) {
							return false;
						} else {
							row.value1 = $("#sel_dateFrom").datebox('getValue');
						}
					}
					
					if ($('#div_sel_dateTo').css("display")!="none") {
						var validate = $('#sel_dateTo').datebox("isValid");
						if (!validate) {
							return false;
						} else {
							row.value2 = $("#sel_dateTo").datebox('getValue');
						}
					}
					
					if ($('#div_sel_dateTimeFrom').css("display")!="none") {
						var validate = $('#sel_dateTimeFrom').datetimebox("isValid");
						if (!validate) {
							return false;
						} else {
							row.value1 = $("#sel_dateTimeFrom").datetimebox('getValue');
						}
					}
					if ($('#div_sel_dateTimeTo').css("display")!="none") {
						var validate = $('#sel_dateTimeTo').datetimebox("isValid");
						if (!validate) {
							return false;
						} else {
							row.value2 = $("#sel_dateTimeTo").datetimebox('getValue');
						}
					}
					if ($('#div_complex_search_dataDic').css("display")!="none") {
						var validate = $('#complex_search_dataDic').combobox("isValid");
						if (!validate) {
							return false;
						} else {
							row.value1 = $("#complex_search_dataDic").combobox('getValue');
						}
					}
					var rowIndex = $('#condition_dg_complex').datagrid('getRowIndex', row);
					$('#condition_dg_complex').datagrid('refreshRow', rowIndex);
				}

			},
	
		/**
		 * 页面初始化函数
		 */
		init : function(pageId, colLst, gridId, url) {
			// 保存DataGrid ID及URL
			complex_search_pageId = pageId;
			complex_search_gridId = gridId;
			complex_search_url = url;
			var dataDicCode = null;
			var dataDicValue = null;
			// 创建预定义查询DataGrid
			$('#predefine_dg_complex').datagrid({
				url: 'seniorQuery/getPredefineSearchList.json',
				method: 'get',
				iconCls: 'icon-reload',
				queryParams: {pageId: pageId, Login_SessionId : sessionId, readOnly : 'YES'},
				pagination:false,
				singleSelect: true,
				striped:true,
				fitColumns:true,
				showHeader: false,
				columns:[[
                          {field:'id',hidden:true},
                          {field:'userCode',hidden:true},
                          {field:'pageId',hidden:true},
                          {field:'columnType',hidden:true},
						  {field:'queryName',title:'查询名',sortable:true,width:200,align:'left'}
						]],
						
				onRowContextMenu: function(e,index,row){
	                e.preventDefault();
					$('#complexSearchPage_menu').menu('show', {
				        left: e.pageX,
				        top: e.pageY
				    });
			    },
						
				onSelect: function(rowIndex, rowData){
					var userCode = rowData.userCode;
					var pageId = rowData.pageId;
					var queryName = rowData.queryName;
					if (queryName == null || queryName == undefined) {
						return false;
					}
					
					// 设置预定义条件列表DataGrid的URL
					var url = "seniorQuery/getPredefineConditionList.json";
					$('#condition_dg_complex').datagrid('options').url = url;
					
					var param = {
							userCode : userCode,
							pageId : pageId,
							queryName : queryName,
							Login_SessionId : sessionId,
							readOnly : 'YES'
						};
					
					// 刷新数据
					$('#condition_dg_complex').datagrid("load", param);
				}
			});	

			// 初始化列名下拉框
			/**
			 * 传过来的列定义形式
			var colLst = [{
				columnName: '订单编号',
				type:  'CHAR'
			  },{
				  columnName: '建档号',
					type:  'NUMBER'
			  },{
				  columnName: '日期',
					type:  'DATE'
			  },{
				  columnName: '创建时间',
					type:  'DATETIME'
			  }];
			  **/
			$('#complex_search_colName').combobox({
				data: colLst,
			    valueField:'columnName',
			    editable:false,
			    textField:'columnName',
			    value: colLst[0].columnName,
			    
				onSelect: function(data){
					
					var columnType = data.type;
					var sign = $('#complex_search_compare').combobox('getValue');
					dataDicCode = data.codeType;
					switch (columnType) {
					case "DATE":
						if (sign == "介于...之间") {
							$("#div_complex_search_colValue1").css("display","none");
							$("#div_complex_search_colValue2").css("display","none");
							$("#div_complex_search_dateFrom").css("display","block");
							$("#div_complex_search_dateTo").css("display","block");
							$("#div_complex_search_dateTimeFrom").css("display","none");
							$("#div_complex_search_dateTimeTo").css("display","none");
							//$("#div_complex_search_dataDic").css("display","none");
						} else {
							$("#div_complex_search_colValue1").css("display","none");
							$("#div_complex_search_colValue2").css("display","none");
							$("#div_complex_search_dateFrom").css("display","block");
							$("#div_complex_search_dateTo").css("display","none");
							$("#div_complex_search_dateTimeFrom").css("display","none");
							$("#div_complex_search_dateTimeTo").css("display","none");
							//$("#div_complex_search_dataDic").css("display","none");
						}
						break;
					case "DATETIME":
						if (sign == "介于...之间") {
							$("#div_complex_search_colValue1").css("display","none");
							$("#div_complex_search_colValue2").css("display","none");
							$("#div_complex_search_dateFrom").css("display","none");
							$("#div_complex_search_dateTo").css("display","none");
							$("#div_complex_search_dateTimeFrom").css("display","block");
							$("#div_complex_search_dateTimeTo").css("display","block");
							//$("#div_complex_search_dataDic").css("display","none");
						} else {
							$("#div_complex_search_colValue1").css("display","none");
							$("#div_complex_search_colValue2").css("display","none");
							$("#div_complex_search_dateFrom").css("display","none");
							$("#div_complex_search_dateTo").css("display","none");
							$("#div_complex_search_dateTimeFrom").css("display","block");
							$("#div_complex_search_dateTimeTo").css("display","none");
							//$("#div_complex_search_dataDic").css("display","none");
						}
						break;

					default:
						if (dataDicCode != null && (sign == "等于" || sign == "不等于")) {
							$("#div_complex_search_colValue1").css("display","none");
							$("#div_complex_search_colValue2").css("display","none");
							$("#div_complex_search_dateFrom").css("display","none");
							$("#div_complex_search_dateTo").css("display","none");
							$("#div_complex_search_dateTimeFrom").css("display","none");
							$("#div_complex_search_dateTimeTo").css("display","none");
						   $("#div_complex_search_dataDic").css("display","block");
						
						$.ajax({
							type: 'post',
							url: 'seniorQuery/getDataDicList.json?codeType=' + dataDicCode + '&Login_SessionId=' + sessionId,
							dataType: 'json',
							success :function(retData){
								if (retData != null) {
									var dataList = [];
									$.each(retData, function(i,item){
										dataList.push({"id":this,"text":this});
									});
									var selectValue = dataList[0].id;
									if (dataDicValue != null) {
										selectValue =dataDicValue;
									}
									$('#complex_search_dataDic').combobox({
										data: dataList,
										value: selectValue,
									});
								}
							}
						});
					} else {
							$("#div_complex_search_dataDic").css("display","none");
								if (sign == "介于...之间") {
									$("#div_complex_search_colValue1").css("display","block");
									$("#div_complex_search_colValue2").css("display","block");
									$("#div_complex_search_dateFrom").css("display","none");
									$("#div_complex_search_dateTo").css("display","none");
									$("#div_complex_search_dateTimeFrom").css("display","none");
									$("#div_complex_search_dateTimeTo").css("display","none");
								} else {
									$("#div_complex_search_colValue1").css("display","block");
									$("#div_complex_search_colValue2").css("display","none");
									$("#div_complex_search_dateFrom").css("display","none");
									$("#div_complex_search_dateTo").css("display","none");
									$("#div_complex_search_dateTimeFrom").css("display","none");
									$("#div_complex_search_dateTimeTo").css("display","none");
								}
					 }
					 break;	
						
					}
				}
			    
			});
			

			// 创建比较符数据
			var signLst = $.ComplexSearchUtils.createCompareSigns();
			
			// 比较符下拉框
			$('#complex_search_compare').combobox({
				data: signLst,
			    valueField:'value',
			    textField:'name',
			    editable:false,
			    value: signLst[0].value,
			    
				onSelect: function(data){
					var sign = data.value;
					var columnType = $.ComplexSearchUtils.getColType();
					
					if (sign == "为空" || sign == "不为空") {
						$("#div_complex_search_colValue1").css("display","none");
						$("#div_complex_search_colValue2").css("display","none");
						$("#div_complex_search_dateFrom").css("display","none");
						$("#div_complex_search_dateTo").css("display","none");
						$("#div_complex_search_dateTimeFrom").css("display","none");
						$("#div_complex_search_dateTimeTo").css("display","none");
						$("#div_complex_search_dataDic").css("display","none");
						$("#div_complex_search_dataDic").css("display","none");
						return true;
					}
					
					if (sign == "介于...之间") {
						$("#div_complex_search_dataDic").css("display","none");
						if (columnType == "DATE") {
							$("#div_complex_search_colValue1").css("display","none");
							$("#div_complex_search_colValue2").css("display","none");
							$("#div_complex_search_dateFrom").css("display","block");
							$("#div_complex_search_dateTo").css("display","block");
							$("#div_complex_search_dateTimeFrom").css("display","none");
							$("#div_complex_search_dateTimeTo").css("display","none");
						} else if (columnType == "DATETIME"){
							$("#div_complex_search_colValue1").css("display","none");
							$("#div_complex_search_colValue2").css("display","none");
							$("#div_complex_search_dateFrom").css("display","none");
							$("#div_complex_search_dateTo").css("display","none");
							$("#div_complex_search_dateTimeFrom").css("display","block");
							$("#div_complex_search_dateTimeTo").css("display","block");
						} else {
							$("#div_complex_search_colValue1").css("display","inline");
							$("#div_complex_search_colValue2").css("display","inline");
							$("#div_complex_search_dateFrom").css("display","none");
							$("#div_complex_search_dateTo").css("display","none");
							$("#div_complex_search_dateTimeFrom").css("display","none");
							$("#div_complex_search_dateTimeTo").css("display","none");
						}
					} else {
						if (columnType == "DATE") {
							$("#div_complex_search_colValue1").css("display","none");
							$("#div_complex_search_colValue2").css("display","none");
							$("#div_complex_search_dateFrom").css("display","block");
							$("#div_complex_search_dateTo").css("display","none");
							$("#div_complex_search_dateTimeFrom").css("display","none");
							$("#div_complex_search_dateTimeTo").css("display","none");
							$("#div_complex_search_dataDic").css("display","none");
						} else if (columnType == "DATETIME"){
							$("#div_complex_search_colValue1").css("display","none");
							$("#div_complex_search_colValue2").css("display","none");
							$("#div_complex_search_dateFrom").css("display","none");
							$("#div_complex_search_dateTo").css("display","none");
							$("#div_complex_search_dateTimeFrom").css("display","block");
							$("#div_complex_search_dateTimeTo").css("display","none");
							$("#div_complex_search_dataDic").css("display","none");
						} else {
							if (dataDicCode != null && (sign == "等于" || sign == "不等于")) {
								$("#div_complex_search_dataDic").css("display","block");
								$("#div_complex_search_colValue1").css("display","none");
								$("#div_complex_search_colValue2").css("display","none");
								$("#div_complex_search_dateFrom").css("display","none");
								$("#div_complex_search_dateTo").css("display","none");
								$("#div_complex_search_dateTimeFrom").css("display","none");
								$("#div_complex_search_dateTimeTo").css("display","none");
								$.ajax({
									type: 'post',
									url: 'seniorQuery/getDataDicList.json?codeType=' + dataDicCode + '&Login_SessionId=' + sessionId,
									dataType: 'json',
									success :function(retData){
										if (retData != null) {
											var dataList = [];
											$.each(retData, function(i,item){
												dataList.push({"id":this,"text":this});
											});
											var selectValue = dataList[0].id;
											//if (dataDicValue != null) {
										//		selectValue =dataDicValue;
										//	}
											$('#complex_search_dataDic').combobox({
												data: dataList,
												value: selectValue,
											});
										}
									}
								});
							} else {
								$("#div_complex_search_colValue1").css("display","block");
								$("#div_complex_search_colValue2").css("display","none");
								$("#div_complex_search_dateFrom").css("display","none");
								$("#div_complex_search_dateTo").css("display","none");
								$("#div_complex_search_dateTimeFrom").css("display","none");
								$("#div_complex_search_dateTimeTo").css("display","none");
								$("#div_complex_search_dataDic").css("display","none");
							}
						
							
						}
					}
					
				}
			});
			
			// 创建预定义条件列表DataGrid
			$('#condition_dg_complex').datagrid({
				url: '',
				method: 'get',
				queryParams: {Login_SessionId : sessionId},
				iconCls: 'icon-reload',
				pagination:false,
				singleSelect: true,
				striped:true,
				fitColumns:true,
				showHeader: true,
				columns:[[
                            {field:'id',hidden:true},
                            {field:'userCode',hidden:true},
                            {field:'pageId',hidden:true},
                            {field:'columnType',hidden:true},
							{field:'andor',title:'关系',width:200,align:'left'},
				            {field:'columnName',title:'列名',width:200,align:'left'},
							{field:'condition',title:'比较符',width:200,align:'left'},
							{field:'value1',title:'值1',width:200,align:'left'},
							{field:'value2',title:'值2',width:200,align:'left'}
						]],
						
				onRowContextMenu: function(e,index,row){
	                e.preventDefault();
					$('#complexSearchCondition_menu').menu('show', {
				        left: e.pageX,
				        top: e.pageY
				    });
			    },

				onSelect: function(rowIndex, rowData){
					document.getElementById("sel_colName").innerHTML=rowData.columnName;
					document.getElementById("sel_compareName").innerHTML=rowData.condition;
					var columnType = rowData.columnType;
					var sign = rowData.condition;
					
					
					if (columnType=="DATE") {
						if (sign == "介于...之间") {
							$("#div_sel_colValue1").css("display","none");
							$("#div_sel_colValue2").css("display","none");
							$("#div_sel_dateFrom").css("display","block");
							$("#div_sel_dateTo").css("display","block");
							$("#div_sel_dateTimeFrom").css("display","none");
							$("#div_sel_dateTimeTo").css("display","none");
							$("#sel_dateFrom").datebox('setValue', rowData.value1);
							$("#sel_dateTo").datebox('setValue', rowData.value2);
						} else {
							$("#div_sel_colValue1").css("display","none");
							$("#div_sel_colValue2").css("display","none");
							$("#div_sel_dateFrom").css("display","block");
							$("#div_sel_dateTo").css("display","none");
							$("#div_sel_dateTimeFrom").css("display","none");
							$("#div_sel_dateTimeTo").css("display","none");
							$("#sel_dateFrom").datebox('setValue', rowData.value1);
						}
					} else if (columnType=="DATETIME") {
						if (sign == "介于...之间") {
							$("#sel_dateTimeFrom").datetimebox('setValue', rowData.value1);
							$("#sel_dateTimeTo").datetimebox('setValue', rowData.value2);
							$("#div_sel_colValue1").css("display","none");
							$("#div_sel_colValue2").css("display","none");
							$("#div_sel_dateFrom").css("display","none");
							$("#div_sel_dateTo").css("display","none");
							$("#div_sel_dateTimeFrom").css("display","block");
							$("#div_sel_dateTimeTo").css("display","block");
						} else {
							$("#sel_dateTimeFrom").datetimebox('setValue', rowData.value1);
							$("#div_sel_colValue1").css("display","none");
							$("#div_sel_colValue2").css("display","none");
							$("#div_sel_dateFrom").css("display","none");
							$("#div_sel_dateTo").css("display","none");
							$("#div_sel_dateTimeFrom").css("display","block");
							$("#div_sel_dateTimeTo").css("display","none");
						}
					} else {
						if (sign == "介于...之间") {
							$("#sel_colValue1").val(rowData.value1);
							$("#sel_colValue2").val(rowData.value2);
							$("#div_sel_colValue1").css("display","block");
							$("#div_sel_colValue2").css("display","block");
							$("#div_sel_dateFrom").css("display","none");
							$("#div_sel_dateTo").css("display","none");
							$("#div_sel_dateTimeFrom").css("display","none");
							$("#div_sel_dateTimeTo").css("display","none");
						} else {
							$("#sel_colValue1").val(rowData.value1);
							$("#div_sel_colValue1").css("display","block");
							$("#div_sel_colValue2").css("display","none");
							$("#div_sel_dateFrom").css("display","none");
							$("#div_sel_dateTo").css("display","none");
							$("#div_sel_dateTimeFrom").css("display","none");
							$("#div_sel_dateTimeTo").css("display","none");
						}
					}
					
				}
						
			});	

		}
		
	};	
})(jQuery);
